package org.kdev.wtf;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigReader
{

   private final Properties properties = new Properties();
   private static String userConfig = "src/test/java/configs/localconfig.properties";

   /**
    * Loads a Properties file.
    * 
    * @param configFileName
    *        properties file path
    * @throws IOException
    *         if an error occurred when reading from the Properties file.
    */
   public ConfigReader(String configFileName) throws IOException
   {
      FileInputStream configFis = null;
      try
      {
         if (new File(userConfig).exists())
         {
            configFis = new FileInputStream(new File(userConfig));
            properties.load(configFis);
         }
         else
         {
            configFis = new FileInputStream(new File(configFileName));
            properties.load(configFis);
         }
      }
      finally
      {
         if (configFis != null)
            configFis.close();
      }
   }

   /**
    * App server url.
    * 
    * @return
    */
   public String getAppServerUrl()
   {
      return properties.getProperty("appServerUrl");
   }

   /**
    * FireFox is default browser
    * 
    * @return
    */
   public String getBrowserStartCommand()
   {
      return properties.getProperty("browserStartCommand", "*firefox");
   }

   public String getBrowserProfileTemplate()
   {
      return properties.getProperty("browserProfileTemplate");
   }

   public String getSeleniumServerHost()
   {
      return properties.getProperty("seleniumServerHost");
   }

   public int getSeleniumServerPort()
   {
      return Integer.parseInt(properties.getProperty("seleniumServerPort"));
   }

   /**
    * App context relative to app server url. AppServerUrl + AppContext = full
    * application server url
    * 
    * @return
    */
   public String getAppContext()
   {
      return properties.getProperty("appContext");
   }

   /**
    * AppLogin class which is used to login to application. Return empty string
    * means that no login is required.
    * 
    * @return
    */
   public String getAppManagerClass()
   {
      // default is framework AppProxy
      return properties.getProperty("appManagerClass", "org.kdev.wtf.AppManager");
   }

   public String getLogFilePath()
   {
      return properties.getProperty("logFilePath");
   }

   public String getDownloadPath()
   {
      return properties.getProperty("downloadPath");
   }

   public String getTimeout()
   {
      return properties.getProperty("timeout");
   }
}
